package com.lysj.bhp.article.controller;

import com.lysj.bhp.activity.domain.Activity;
import com.lysj.bhp.article.base.ArticleContent;
import com.lysj.bhp.article.domain.Article;
import com.lysj.bhp.article.vo.ArticleVo;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("admin/article")
public class ArticleController extends ArticleContent {

    @InitBinder
    public void InitBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @GetMapping("list")
    public Resp list() {

        return new Resp(articleRepository.findByIsExistOrderByDateDesc(1));
    }

    @PostMapping("add")
    public Resp add(Article model) {
//        if (ParamUtil.isBlack(model.getChineseTitle())) {
//            return new Resp(Resp.PARAM_ERROR, "中文标题不能为空");
//        }
//        if (ParamUtil.isBlack(model.getEnglishTitle())) {
//            return new Resp(Resp.PARAM_ERROR, "英文标题不能为空");
//        }
//        if (ParamUtil.isBlack(model.getChineseIntroduce())) {
//            return new Resp(Resp.PARAM_ERROR, "中文简介不能为空");
//        }
//        if (ParamUtil.isBlack(model.getEnglishIntroduce())) {
//            return new Resp(Resp.PARAM_ERROR, "英文简介不能为空");
//        }
//        if (ParamUtil.isBlack(model.getChinesePhoto())) {
//            return new Resp(Resp.PARAM_ERROR, "中文图片不能为空");
//        }
//        if (ParamUtil.isBlack(model.getEnglishPhoto())) {
//            return new Resp(Resp.PARAM_ERROR, "英文图片不能为空");
//        }
//        if (ParamUtil.isBlack(model.getDate())) {
//            return new Resp(Resp.PARAM_ERROR, "时间不能为空");
//        }
//        if (ParamUtil.isBlack(model.isWhether())) {
//            return new Resp(Resp.PARAM_ERROR, "请选择是否外链");
//        }
//        if (model.isWhether() && ParamUtil.isBlack(model.getChineseUrl())) {
//            return new Resp(Resp.PARAM_ERROR, "中文外链URL不能为空");
//        }
//        if (model.isWhether() && ParamUtil.isBlack(model.getEnglishUrl())) {
//            return new Resp(Resp.PARAM_ERROR, "英文外链URL不能为空");
//        }
        model.setIsExist(1);
        articleRepository.save(model);
        return new Resp("添加成功");
    }

    @PostMapping("/edit")
    public Resp edit(Article model) {
        Article article = articleRepository.findOne(model.getArticleId());
        if (article == null) {
            return new Resp(Resp.PARAM_ERROR, "文章不存在!");
        }
//        if (ParamUtil.isBlack(model.getChineseTitle())) {
//            return new Resp(Resp.PARAM_ERROR, "中文标题不能为空");
//        }
//        if (ParamUtil.isBlack(model.getEnglishTitle())) {
//            return new Resp(Resp.PARAM_ERROR, "英文标题不能为空");
//        }
//        if (ParamUtil.isBlack(model.getChineseIntroduce())) {
//            return new Resp(Resp.PARAM_ERROR, "中文简介不能为空");
//        }
//        if (ParamUtil.isBlack(model.getEnglishIntroduce())) {
//            return new Resp(Resp.PARAM_ERROR, "英文简介不能为空");
//        }
//        if (ParamUtil.isBlack(model.getChinesePhoto())) {
//            return new Resp(Resp.PARAM_ERROR, "中文图片不能为空");
//        }
//        if (ParamUtil.isBlack(model.getEnglishPhoto())) {
//            return new Resp(Resp.PARAM_ERROR, "英文图片不能为空");
//        }
//        if (ParamUtil.isBlack(model.getDate())) {
//            return new Resp(Resp.PARAM_ERROR, "时间不能为空");
//        }
//        if (ParamUtil.isBlack(model.isWhether())) {
//            return new Resp(Resp.PARAM_ERROR, "请选择是否外链");
//        }
//        if (model.isWhether() && ParamUtil.isBlack(model.getChineseUrl())) {
//            return new Resp(Resp.PARAM_ERROR, "中文外链URL不能为空");
//        }
//        if (model.isWhether() && ParamUtil.isBlack(model.getEnglishUrl())) {
//            return new Resp(Resp.PARAM_ERROR, "英文外链URL不能为空");
//        }
        article.setChineseTitle(model.getChineseTitle());
        article.setEnglishTitle(model.getEnglishTitle());
        article.setChineseIntroduce(model.getChineseIntroduce());
        article.setEnglishIntroduce(model.getEnglishIntroduce());
        article.setChinesePhoto(model.getChinesePhoto());
        article.setEnglishPhoto(model.getEnglishPhoto());
        article.setDate(model.getDate());
        article.setWhether(model.isWhether());
        article.setChineseUrl(model.getChineseUrl());
        article.setEnglishUrl(model.getEnglishUrl());
        articleRepository.save(article);
        return new Resp("修改成功");
    }

    @PostMapping("/remove")
    public Resp remove(Article model) {
        if (ParamUtil.isBlack(model.getArticleId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择文章");
        }
        Article article = articleRepository.findOne(model.getArticleId());
        if (null == article) {
            return new Resp(Resp.PARAM_ERROR, "查无此文章");
        }
        article.setIsExist(2); //设定状态为2,用于逻辑删除
        articleRepository.save(article);
        return new Resp("删除成功");
    }




    @PostMapping("/editChineseContent")
    public Resp editChineseContent(Long articleId, String chineseContent) {
        if (ParamUtil.isBlack(articleId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择文章");
        }
        Article article = articleRepository.findOne(articleId);
        if (null == article) {
            return new Resp(Resp.PARAM_ERROR, "查无此文章");
        }

        article.setChineseContent(chineseContent);
        articleRepository.save(article);
        return new Resp("添加成功");
    }

    @PostMapping("/editEnglishContent")
    public Resp editEnglishContent(Long articleId, String englishContent) {
        if (ParamUtil.isBlack(articleId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择文章");
        }
        Article article = articleRepository.findOne(articleId);
        if (null == article) {
            return new Resp(Resp.PARAM_ERROR, "查无此文章");
        }

        article.setEnglishContent(englishContent);
        articleRepository.save(article);
        return new Resp("添加成功");
    }


}
